package com.yulongtian.offers.day202209.day20220923;

/**
 * https://leetcode.cn/problems/he-wei-sde-liang-ge-shu-zi-lcof/?favorite=xb9nqhhg
 *
 * @author YulongTian
 * @date 2022/9/23 - 17:07
 */
public class Test07 {
    public static void main(String[] args) {
        int[] nums = {10,26,30,31,47,60};
        int target = 40;
        int[] ints = twoSum(nums, target);
        System.out.print(ints[0] + " ");
        System.out.println(ints[1]);
    }

    public static int[] twoSum(int[] nums, int target) {
        int[] res = new int[2];
        int index = 0;
        while (index < nums.length) {
            int num1 = nums[index];
            int num2 = target - num1;

            int left = index + 1;
            int right = nums.length - 1;
            boolean flag = false;
            while (left < right) {
                if(num2==nums[left]||num2==nums[right]){
                    res[0] = num1;
                    res[1] = num2;
                    return res;
                }

                int mid = (left + right) / 2;
                if (nums[mid] == num2) {
                    res[0] = num1;
                    res[1] = num2;
                    flag = true;
                    break;
                }

                if (nums[mid] < num2) {
                    left = mid + 1;
                    continue;
                }
                if (nums[mid] > num2) {
                    right = mid - 1;
                }
            }
            index++;
            if (flag) {
                break;
            }
        }
        if (res[1] == 0) {
            return new int[]{};
        }
        return res;
    }

}
